<?php
/**
 * (c) 2010 Philip Clarke, Bouncing Limited.
**/

define('BOX_TOOLS_AUTO_UPDATE_MENU_TEXT', 'Automatic Updates');
define('FILENAME_AUTO_UPDATE', 'auto_update.php');
define('TABLE_AUTO_UPDATE', DB_PREFIX.'auto_update');
define('TABLE_AUTO_UPDATE_LOG', DB_PREFIX.'auto_update_log');
define('TABLE_AUTO_UPDATE_AVAILABLE', DB_PREFIX.'auto_update_available');
define('TABLE_MODULES_OPTIONAL', DB_PREFIX.'modules_optional');
define('TABLE_MODULES_SCHEDULED', DB_PREFIX.'modules_scheduled');
define('TABLE_PATCHES', DB_PREFIX.'patches');
define('TABLE_PATCHES_SCHEDULED', DB_PREFIX.'patches_scheduled');
define('TABLE_AUTO_UPDATE_LOG', DB_PREFIX.'auto_update_log');
define('TABLE_AUTO_UPDATE_AVAILABLE', DB_PREFIX.'auto_update_available');
define('DIR_FS_AUTO_UPDATE_DOWNLOAD', DIR_FS_ADMIN.'updates/downloads/');
define('DIR_FS_AUTO_UPDATE_WORKSPACE', DIR_FS_ADMIN.'updates/workspace/');
define('DIR_FS_AUTO_UPDATE_OUTPUT', DIR_FS_ADMIN.'updates/output/');
define('MODULE_AUTO_UPDATE_NOZIP', 'This shop can only download updates, it does not have the correct configuration to unzip the files.');
define('MODULE_AUTO_UPDATE_LOCATION', DIR_WS_ADMIN);

if(!class_exists('smz_filesystem')){
  die("This module requires a ShopUpgraded installation for auto update to function.<br />This error message was generated by <em>".__FILE__."</em> line no:".__LINE__."<br /> Removing this line will probably do <strong>severe damage</strong> to your Zen Cart shop as the auto update module is specifically designed to execute, replace, overwrite, modify and unzip files on a ShopUpgraded system.");
}


$sql = array();

$fl = $fc;


// Admin folder is renamed, AUTO_UPDATE_LOCATION resets (since DIR_WS_ADMIN is hard coded in configure.php).

if(defined('MODULE_AUTO_UPDATE_LOCATION') && MODULE_AUTO_UPDATE_LOCATION != DIR_WS_ADMIN ){

  $sql[] = "UPDATE ".TABLE_CONFIGURATION." SET configuration_value = '".DIR_WS_ADMIN."' WHERE configuration_key='MODULE_AUTO_UPDATE_LOCATION'";

  define('AUTO_UPDATE_LOCATION', DIR_WS_ADMIN);

}else{

  define('AUTO_UPDATE_LOCATION', MODULE_AUTO_UPDATE_LOCATION);

}


if(!defined('MODULE_AUTO_UPDATE_EMAIL') || trim(MODULE_AUTO_UPDATE_EMAIL)==''){

$sql[] = "REPLACE INTO `".TABLE_CONFIGURATION."` (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('Automatic Installation Email','MODULE_AUTO_UPDATE_EMAIL','".STORE_OWNER_EMAIL_ADDRESS."','The email address that receives update/ installation messages',20,NULL,NOW(),NOW(),NULL,NULL)";

}


foreach($sql as $ex){
  $db->execute($ex);
}

unset($sql) ;



if(defined('AUTO_UPDATE_LOCATION')){


  if( (time()-MODULE_AUTO_UPDATE_TIME)>24*60*60  ){

  $sql = "UPDATE `".TABLE_CONFIGURATION."` SET configuration_value='".time()."' WHERE configuration_key='MODULE_AUTO_UPDATE_TIME'";

  $db->Execute($sql);

  $url = HTTP_SERVER.AUTO_UPDATE_LOCATION.'update.php' ;

  $a = parse_url($url);


  $fp = fsockopen( ( isset($_SERVER['HTTPS']) ? 'ssl://'.$a['host'] : $a['host']), (isset($_SERVER['HTTPS']) ? 443 : 80 ), $errno, $errstr, 5);
  if ($fp) {
    $out = "HEAD ".AUTO_UPDATE_LOCATION."update.php HTTP/1.1\r\n";
    $out .= "Host: ".$a['host']."\r\n";
    $out .= "Accept: */*\r\n";
    $out .= "Content-length: 0\r\n\r\n";
    fwrite($fp, $out);
    fclose($fp);
  }

  unset ($a, $status, $url, $sql);

  }

  unset($run_auto_update);


}

if(!in_array('zip', get_loaded_extensions())){
  $messageStack->add(MODULE_AUTO_UPDATE_NOZIP, 'warning');
}



